package org.example.auth.controller;

import org.example.auth.contract.request.UserRequestForCreate;
import org.example.auth.contract.request.UserRequestForUpdatePassword;
import org.example.auth.contract.response.UserResponse;
import org.example.auth.convert.UserConvert;
import org.example.auth.service.UserService;
import org.example.misc.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserConvert userConvert;

    /**
     * 创建用户
     */
    @PostMapping("createUser")
    Result<UserResponse> createUser(@RequestBody  @Validated UserRequestForCreate request) {
        return Result.ok(userConvert.clone(userService.save(request)));
    }

    /**
     * 查询用户列表
     */
    @GetMapping("list")
    Result<List<UserResponse>> getUserList() {
        return Result.ok(userConvert.clone(userService.getUserList()));
    }

    /**
     * 查询用户详情
     */
    @GetMapping("getUserById")
    Result<UserResponse> getUserById(@RequestParam Long id) {
        return Result.ok(userConvert.clone(userService.getUserByIdWithException(id)));
    }

    /**
    /**
     * 修改密码
     */
    @PostMapping("updatePassword")
    Result<Boolean> updatePassword(@RequestBody  @Validated UserRequestForUpdatePassword request) {
        userService.updatePassword(request);
        return Result.ok(Boolean.TRUE);
    }

    /**
     * 删除用户
     */
    @GetMapping("deleteUser")
    Result<Boolean> deleteUser(@RequestParam Long id){
        userService.deleteUser(id);
        return Result.ok(Boolean.TRUE);
    }

}
